package servlet.context;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-07-15
 * Time:0:22
 */
/*
ServletContext是一个接口,Tomcat实现的类是ApplicationContextFacade
ServletContext可以:  1.获取|修改web.xml的配置参数
                    2.获取当前工程路径 格式 -> /工程路径
                    3.获取工程部署在服务器硬盘上的绝对路径  注意,工程会镜像映射在out目录,out目录下才是真正的输出路径
                    4.供多个Servlet共享数据    类似Map,原理参考1
 */
@WebServlet(name = "ServletContext_", urlPatterns = "/servletContext")
public class ServletContext_ extends HttpServlet{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = req.getServletContext();
        String website = servletContext.getInitParameter("website");
        String place = servletContext.getInitParameter("place");
        String contextPath = servletContext.getContextPath();
        String realPath = servletContext.getRealPath("/");
        System.out.println(servletContext);//org.apache.catalina.core.ApplicationContextFacade@2cf4d736
        System.out.println(website);//https://www.weblearning.com
        System.out.println(place);//company
        System.out.println(contextPath);// /Servlet
        System.out.println(realPath);//C:\Users\22954\Desktop\IDEA\java-web\Servlet-Learning\out\artifacts\Servlet_Learning_war_exploded\

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
